package com.lei.demo.activiti.spring.test;

import java.util.List;

import org.activiti.engine.HistoryService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class DemoProcess {
	
	private static ApplicationContext context;

	public static void main(String[] args) {
		
		System.out.println( "start" );
		
		context = new ClassPathXmlApplicationContext("spring-activiti.xml");
		
		//创建Activiti流程引擎，Activiti主接口
		ProcessEngine processEngine = (ProcessEngine) context.getBean("processEngine");
		System.out.println("processEngine Name:"+processEngine.getName());
		
		// 获得Activiti的资源操作接口 
        RepositoryService repositoryService = processEngine.getRepositoryService();
        
        //获得运行时操作接口（大多操作流程实例）
        RuntimeService runtimeService = processEngine.getRuntimeService();
        
        //部署流程定义，部署后，activiti会根据数据库配置自动创建表，添加数据
      	Deployment deployment = repositoryService
      							.createDeployment()
      							.addClasspathResource("diagrams/DemoProcess.bpmn")
      							.deploy();
      	System.out.println("deployment ID:"+deployment.getId()+" deployment Name:"+deployment.getName());
      	
      	//启动流程实例 ,startProcessInstanceByKey参数DemoProcess即DemoProcess.bpmn中的ID
        ProcessInstance instance = processEngine  
                 .getRuntimeService().startProcessInstanceByKey("DemoProcess");  
        String procId = instance.getId();  
        System.out.println("procId:"+ procId);
        
        //获得第一个任务 ，taskDefinitionKey参数为流程途中UserTask的ID
        TaskService taskService = processEngine.getTaskService();  
        List<Task> tasks = taskService.createTaskQuery().taskDefinitionKey("firstTask").list();  
        for (Task task : tasks) {  
            System.out.println("Task信息: taskID -" +task.getId()+" taskName -"+ task.getName());  
            // 认领任务  
            taskService.claim(task.getId(), "testUser");
        }
        
        //完成任务前，输出testUser下的任务数量
        System.out.println("完成任务前，testUser认领的task数量: " 
                + taskService.createTaskQuery().taskAssignee("testUser").count());
        //查看testUser 现在是否能够获取到该任务  
        tasks = taskService.createTaskQuery().taskAssignee("testUser").list();
        for (Task task : tasks) {  
            System.out.println("Task for testUser: " + task.getName());  
            // 完成任务  
            taskService.complete(task.getId());  
        }
        //完成任务后，输出testUser下的任务数量
        System.out.println("完成任务后，testUser认领的task数量: " 
                + taskService.createTaskQuery().taskAssignee("testUser").count());  
          
  
        // 获取并认领第二个任务  
        tasks = taskService.createTaskQuery().taskDefinitionKey("secondTask").list();  
        for (Task task : tasks) {  
            System.out.println("Task信息: taskID -" +task.getId()+" taskName -"+ task.getName());  
            taskService.claim(task.getId(), "testUser");  
        }  
          
        //完成第二个任务结束结束流程  
        for (Task task : tasks) {  
            taskService.complete(task.getId());  
        }
        
     // 核实流程是否结束  
        HistoryService historyService = processEngine.getHistoryService();  
        HistoricProcessInstance historicProcessInstance = historyService
        													.createHistoricProcessInstanceQuery()
        													.processInstanceId(procId)
        													.singleResult();
        System.out.println("流程实例结束时间: " + historicProcessInstance.getEndTime());
		
		System.out.println( "end" );

	}

}
